package com.pn.warehouse.controller;

import com.pn.warehouse.entity.ProductType;
import com.pn.warehouse.entity.Result;
import com.pn.warehouse.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/productCategory")
@RestController
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 查询商品分类树
     */
    @RequestMapping("/product-category-tree")
    public Result productCategoryTree(){
        //1、执行业务并返回
        List<ProductType> productTypes = productTypeService.productTypeTree();
        return Result.ok(productTypes);
    }

    /**
     * 检查分类编码是否已经存在
     */
    @RequestMapping("/verify-type-code")
    public Result checkTypeCode(String typeCode){
        //1、执行业务并返回
        Result result = productTypeService.findTypeByCodeOrName(typeCode);
        return result;
    }

    /**
     * 添加商品的分类
     */
    @RequestMapping("/type-add")
    public Result addProductType(@RequestBody ProductType productType){
        //1、执行业务并返回
        Result result = productTypeService.insertProductType(productType);
        return result;
    }

    /**
     * 删除分类
     */
    @RequestMapping("/type-delete/{typeId}")
    public Result deleteProductType(@PathVariable Integer typeId){
        //1、执行业务并返回
        Result result = productTypeService.removeProductType(typeId);
        return result;
    }

    /**
     * 修改商品的分类
     */
    @RequestMapping("/type-update")
    public Result updateProductType(@RequestBody ProductType productType){
        //1、执行业务并返回
        Result result = productTypeService.setProductTypeById(productType);
        return result;
    }

}
